note
    description: "A thread that is responsible for a single client. May serve multiple requests"
    author: "zistvan"
    date: "$Date$"
    revision: "$Revision$"

class
    CONNECTION_THREAD

create
    make,
    make_with_own_logger

feature {NONE} -- init
    sock             : HACKED_STREAM_SOCKET
    id                 : attached INTEGER
    logger_instance : separate LOGGER
    output_enabled     : BOOLEAN
    logging_enabled : BOOLEAN

    make (tid : INTEGER; socket_desc: INTEGER; lgr : separate LOGGER;out_enabled : BOOLEAN; log_enabled : BOOLEAN)
    -- create with an ID and a file descriptor
    do

        create sock.create_from_descriptor (socket_desc)
        logger_instance := lgr
        id := tid
        output_enabled := out_enabled
        logging_enabled := log_enabled
    end

    make_with_own_logger (tid : INTEGER; socket_desc: INTEGER; out_enabled : BOOLEAN; log_enabled : BOOLEAN)
    -- create with an ID and a file descriptor
    do

        create sock.create_from_descriptor (socket_desc)
        create logger_instance.make
        id := tid
        output_enabled := out_enabled
        logging_enabled := log_enabled
    end

    internal_serve (socket : NETWORK_STREAM_SOCKET)
    local
        req     : REQUEST
        done    : BOOLEAN
    do

        from done := false
        until done = true
        loop
            if socket = Void or socket.bad_socket_handle then
                -- this means something has gone wrong. quit.
                done := true

                if output_enabled then
                    print ("[CLIENT#"+id.out+"] Error while serving: invalidated socket")
                    io.put_new_line
                end
                if logging_enabled then
                    log(logger_instance, "[CLIENT#"+id.out+"] Error while serving: invalidated socket")
                end
            else
                -- create request to read from socket and answer:
                create req.make(socket)

                if req.read_from_socket = true then
                    --the request did not timeout
                    
                    req.parse_and_serve

                    if(req.is_persistent = false) then
                        done:=true
                    end

                    if output_enabled then
                         print("[CLIENT#"+id.out+"] Served file "+ req.filename.out+" with status "+req.status.out+"%N")
                    end
                    if logging_enabled then
                        log(logger_instance, "[CLIENT#"+id.out+"] Served file "+ req.filename.out+" with status "+req.status.out+"")
                    end


                else
                    --socket timed out / connection was reset by client
                    done := true;
                end
            end
        end

           if output_enabled then
               print ("[CLIENT#"+id.out+"] Closing socket.%N")
          end

           if logging_enabled then
            log(logger_instance, "[CLIENT#"+id.out+"] Closing socket.")
           end
        socket.close

    end

    log(logger : separate LOGGER; entry : STRING)
    do
        logger.log(entry)
    end

feature
    --public

    serve
    -- this is called by the conn.pool
    do
        internal_serve (sock)
    end
end
